<?php
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

use Hyperf\Database\Model\Builder;

if (!function_exists('toTree')) {
    /**
     * 无限极分类树
     *
     * @param array $data_list
     * @param int $parent_id
     * @return array
     */
    function toTree(array $data_list, int $parent_id = 0) :array
    {
        $tree = [];

        foreach ($data_list as $item) {
            if ($item['pid'] !== $parent_id) continue;

            foreach ($data_list as $sub) {
                if($sub['pid'] === $item['id']) {
                    $children = toTree($data_list, $item['id']);
                    $item['children'] = $children;
                    break;
                }
            }
            $tree[] = $item;
        }

        return $tree;
    }
}

if (!function_exists('uid')) {
    /**
     * 获取用户ID
     *
     * @param bool $hold 是否登陆拦截 default:true
     * @return int
     */
    function uid(bool $hold = true)
    {
        if ($hold === false) {
            try {
                return (int)\App\Kernel\Util\UserJwtInstance::instance()->build()->getId();
            } catch (CustomException $e) {
                return null;
            }
        } else {
            return (int)\App\Kernel\Util\UserJwtInstance::instance()->build()->getId();
        }
    }
}

if (!function_exists('queryWhere')) {
    /**
     * 生成查询条件
     *
     * @param Builder $query
     * @param array $maps
     * @return Builder
     */
    function queryWhere(Builder $query, array $maps)
    {
        foreach ($maps as $field => $item) {
            // 普通查询
            if (!is_array($item)) {
                $query->where($field, $item);
                continue;
            }

            // 其它方式
            switch (strtolower($item[0])) {
                case 'in':
                    $query->whereIn($field, $item[1]);
                    break;

                default:
                    $query->where($field, $item[0], $item[1]);
            }
        }

        return $query;
    }
}

if (!function_exists('gConfig')) {
    /**
     * 获取配置
     *
     * @param string $name 配置名
     * @param null $default 默认值
     * @return mixed
     */
    function gConfig(string $name, $default = null)
    {
        $config = \Hyperf\Utils\ApplicationContext::getContainer()->get(\App\Service\ConfigService::class);

        return config($name) ?: $config->get($name, $default);
    }
}

if (!function_exists('createTree')) {
    /**
     * 无限极分类树
     *
     * @param array $data_list
     * @param int $parent_id
     * @return array
     */
    function createTree(array $data_list, int $parent_id = 0, array $display_field = [], string $children_name = 'children') :array
    {
        $tree = [];

        foreach ($data_list as $item) {
            if ($item['pid'] !== $parent_id) continue;

            foreach ($data_list as $sub) {
                if($sub['pid'] === $item['id']) {
                    $children = createTree($data_list, $item['id'], $display_field, $children_name);
                    $item[$children_name] = $children;
                    break;
                }
            }

            // 过滤不显示的字段
            foreach($item as $key => $val) {
                if (! in_array($key, $display_field)) {
                    unset($item[$key]);
                }
            }

            $tree[] = $item;
        }

        return $tree;
    }
}